package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.Exception.ErrorCode;
import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.AuditBaseDomain;
import cn.wolfcode.p2p.base.query.RechargeOfflineQueryObject;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.business.domain.RechargeOffline;
import cn.wolfcode.p2p.business.mapper.RechargeOfflineMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.business.service.IRechargeOfflineService;
import cn.wolfcode.p2p.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RechargeOfflineServiceImpl implements IRechargeOfflineService {
    @Autowired
    private RechargeOfflineMapper rechargeOfflineMapper;

    @Autowired
    private IAccountService accountService;

    @Autowired
    private IAccountFlowService accountFlowService;


    @Override
    public void rechargeOffline(RechargeOffline rechargeOffline) {
        //参数校验
        //银行
        AssertUtil.instance()
                .isNotNull(rechargeOffline.getBankInfo().getId(), ErrorCode.BANK_INFO_ERROR)
        //交易时间
        .isNotNull(rechargeOffline.getTradeTime(), ErrorCode.TRADETIME_IS_EMPTY)
        //充值金额
        .isNotNull(rechargeOffline.getAmount(),ErrorCode.RECHARGEAMOUNT_IS_EMPTY)
        //说明
        .isFalse(!StringUtil.emptyString(rechargeOffline.getNote()),ErrorCode.DESCRIPTION_IS_EMPTY)
        //交易号
            .isNotNull(rechargeOffline.getTradeCode(), ErrorCode.TRADECODE_IS_EMPTY);
        //判断用户是否重复提交了,已经该单号是否已经使用过了
        int[] states = {AuditBaseDomain.AUTH_SUCCESS,AuditBaseDomain.AUTH_WAIT};
        int count = rechargeOfflineMapper.selectByTradeCode(states,rechargeOffline.getTradeCode());
        AssertUtil.instance().isFalse(count != 0,ErrorCode.TRANSACTION_NUMBER_ALREADY_EXISTS);
        //设置参数
        apply(rechargeOffline);
    }

    @Override
    public void apply(RechargeOffline rechargeOffline){
        RechargeOffline temp = new RechargeOffline();
        temp.setTradeCode(rechargeOffline.getTradeCode());
        temp.setTradeTime(rechargeOffline.getTradeTime());
        temp.setAmount(rechargeOffline.getAmount());
        temp.setNote(rechargeOffline.getNote());
        temp.setBankInfo(rechargeOffline.getBankInfo());
        temp.setApplyTime(new Date());
        temp.setApplier(UserContext.getLoginInfo());
        rechargeOfflineMapper.insert(temp);
    }

    @Override
    public void update(RechargeOffline rechargeOffline) {
        rechargeOfflineMapper.updateByPrimaryKey(rechargeOffline);
    }


    @Override
    public PageResult queryRechargeList(RechargeOfflineQueryObject qo) {

        int count = rechargeOfflineMapper.queryForCount(qo);
        if (count == 0) {
            return PageResult.empty(qo.getPageSize());
        }
        int totalPage = count % qo.getPageSize() == 0 ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        if(totalPage < qo.getCurrentPage()){
            qo.setCurrentPage(totalPage);
        }
        List<RechargeOffline> listData = rechargeOfflineMapper.queryForList(qo);
        return new PageResult(listData,count,qo.getCurrentPage(),qo.getPageSize());

    }

    @Override
    public void rechargeOfflineAudit(Long id, String remark, Integer state) {
        //判断该充值对象是否处在审核状态
        RechargeOffline rechargeOffline = rechargeOfflineMapper.selectByPrimaryKey(id);
        AssertUtil.instance().isFalse(rechargeOffline.getState() != RechargeOffline.AUTH_WAIT,ErrorCode.HAVE_TO_REVIEW);
        //设置审核信息
        rechargeOffline.setAuditor(UserContext.getLoginInfo());
        rechargeOffline.setAuditTime(new Date());
        //设置审核对象的状态
        rechargeOffline.setState(state);
        rechargeOffline.setRemark(remark);
        update(rechargeOffline);
        if(state == AuditBaseDomain.AUTH_SUCCESS){
        //审核成功
        //将充值对象的状态设置为审核成功
        //将充值的金额加入到用户的可用余额
            Account account = accountService.getAccount(rechargeOffline.getApplier().getId());
            account.addUsableAmount(rechargeOffline.getAmount());
            accountService.update(account);
            //增加一条流水
            accountFlowService.createRechargeOfflineSuccessFlow(rechargeOffline,account);

        }
        //审核失败
        //修改审核对象的状态为审核失败
        //通知资金托管的银行将充值金额退还给用户
        //更新
    }

}
